<?php
Class PuceDAO
{
	public $connexion;

	public function __construct()
	{
		try {
			$this->connexion = new PDO("mysql:host=".SERVEUR_MYSQL.";dbname=".BASE.";charset=utf8", UTILISATEUR, M2P);
		}
		catch(PDOException $e)
		{
			echo "Erreur : ".$e->getMessage();
			die();
		}
	}
	
	public function verifier_existance_puce_by_iddossier( $iddossier) {
		$query = "SELECT count(idpuce) FROM puce WHERE iddossier  = :iddossier ";
		$requette_preparee = $this->connexion->prepare($query);
		$requette_preparee->bindValue(":iddossier" , trim($iddossier), PDO::PARAM_INT);
		$requette_preparee->execute();
		$nbPuce = $requette_preparee->fetchColumn();
		return ($nbPuce==0);
	}
	
	public function rechercher_puce_pour_formulaire($iddossier)
	{
		$query = "SELECT d.idpuce, d.versionhg, d.idtypeprelevement, d.idtypepuce, d.formulechromosomique, d.analyse, d.resolution, d.taillemin,  d.nbmqdeviant, d.qualite, d.iddossier "
				."FROM puce as d ";
		$query .=" WHERE 1 ";
		if ($iddossier != null && $iddossier != 0){
			$query .= " AND d.iddossier = :iddossier";
		}
		$requette_preparee = $this->connexion->prepare($query);
		if ($iddossier != null){
			$requette_preparee->bindValue(":iddossier" , $iddossier, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$puce = $requette_preparee->fetchObject('Puce');
		
		return  $puce;
	
	} // rechercher_puce_pour_formulaire

	public function rechercher_puce($idpuce)
	{
		$query = "SELECT d.idpuce, d.versionhg, d.idtypeprelevement,  d.idtypepuce, d.formulechromosomique, d.analyse, d.resolution, d.taillemin,  d.nbmqdeviant, d.qualite, d.iddossier "
				."FROM puce as d ";
		$query .=" WHERE 1 ";
		if ($idpuce != null && $idpuce != 0){
			$query .= " AND d.idpuce = :idpuce";
		}
		$requette_preparee = $this->connexion->prepare($query);
		if ($idpuce != null){
			$requette_preparee->bindValue(":idpuce" , $idpuce, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$puce = $requette_preparee->fetchObject('Puce');
	
		return  $puce;
	
	} // rechercher_puce
	
	
	public function rechercher_idpuce_par_dossier($iddossier)
	{
		$query = "SELECT d.idpuce"
				." FROM puce as d ";
		$query .=" WHERE 1 ";
		if ($iddossier != null && $iddossier != 0){
			$query .= " AND d.iddossier = :iddossier";
		}
		$requette_preparee = $this->connexion->prepare($query);
		if ($iddossier != null){
			$requette_preparee->bindValue(":iddossier" , $iddossier, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$puce = $requette_preparee->fetch();
		
		return  $puce["idpuce"];
	}
	public function ajouter_puce($puce)
	{
		$message = null;
 		$query = "INSERT INTO puce ( idtypeprelevement, idtypepuce, formulechromosomique, analyse, resolution, taillemin, nbmqdeviant, qualite ,versionhg, iddossier ) values ";
 		$query .= " ( :idtypeprelevement, :idtypepuce, :formulechromosomique, :analyse, :resolution, :analyse, :nbmqdeviant, :qualite ,:versionhg, :iddossier)";
		$requette_preparee = $this->connexion->prepare($query);
		if ($puce->idtypeprelevement == null){
			$requette_preparee->bindValue(":idtypeprelevement" , 0, PDO::PARAM_INT);
		} else {
			$requette_preparee->bindValue(":idtypeprelevement" , $puce->idtypeprelevement, PDO::PARAM_INT);
		}
		if ($puce->idtypepuce == null){
			$requette_preparee->bindValue(":idtypepuce" , 0, PDO::PARAM_INT);
		} else {
			$requette_preparee->bindValue(":idtypepuce" , $puce->idtypepuce, PDO::PARAM_INT);
		}
		if ($puce->formulechromosomique == null){
			$requette_preparee->bindValue(":formulechromosomique" , NULL, PDO::PARAM_NULL);
		} else {
			$requette_preparee->bindValue(":formulechromosomique" , $puce->formulechromosomique, PDO::PARAM_STR);
		}
		if ($puce->analyse == null){
			$requette_preparee->bindValue(":analyse" , NULL, PDO::PARAM_NULL);
		} else {
			$requette_preparee->bindValue(":analyse" , $puce->analyse, PDO::PARAM_STR);
		}
		if ($puce->resolution == null){
 			$requette_preparee->bindValue(":resolution" , NULL, PDO::PARAM_NULL); 
 		} else {
 			$requette_preparee->bindValue(":resolution" , $puce->resolution, PDO::PARAM_STR);
 		}
 		if ($puce->taillemin == null){
 			$requette_preparee->bindValue(":taillemin" , NULL, PDO::PARAM_NULL);
 		} else {
 			$requette_preparee->bindValue(":taillemin" , $puce->taillemin, PDO::PARAM_STR);
 		};
 		if ($puce->nbmqdeviant == null){
 			$requette_preparee->bindValue(":nbmqdeviant" , NULL, PDO::PARAM_NULL);
 		} else {
 			$requette_preparee->bindValue(":nbmqdeviant" , $puce->analyse, PDO::PARAM_STR);
 		};
 		if ($puce->qualite == null){
 			$requette_preparee->bindValue(":qualite" , NULL, PDO::PARAM_NULL);
 		} else {
 			$requette_preparee->bindValue(":qualite" , $puce->qualite, PDO::PARAM_STR);
 		}
 		
 		if ($puce->versionhg == null){
 			$requette_preparee->bindValue(":versionhg" , "hg19", PDO::PARAM_STR);
 		} else {
 			$requette_preparee->bindValue(":versionhg" , $puce->versionhg, PDO::PARAM_STR);
 		}
 		if ($puce->iddossier == null){
 			$requette_preparee->bindValue(":iddossier" , 0, PDO::PARAM_INT);
 		} else {
 			$requette_preparee->bindValue(":iddossier" , $puce->iddossier, PDO::PARAM_INT);
 		}	
  	  	$t = $requette_preparee->execute();
  	  	
  	  	if(!$t)
 		{
 			$taberr = $requette_preparee->errorInfo();
 			$message = "Erreur numéro :".$taberr[0]."(".$taberr[1].")";
 			
  		} else  {
  			$message = $this->connexion->lastInsertId();
  		}
		return $message;
	}//ajouter_puce
	
	public function modifier_puce($puce)
	{
		$message = null;
		$query = "UPDATE puce SET ";
		if ($puce->idtypepuce != null){
			$query.= "idtypepuce = :idtypepuce , ";
		}
		if ($puce->idtypeprelevement != null){
			$query.= "idtypeprelevement = :idtypeprelevement , ";
		}
		if ($puce->formulechromosomique != null){
			$query.= "formulechromosomique = :formulechromosomique , ";
		}
		if ($puce->analyse != null){
			$query.= "analyse = :analyse , ";
		}
		if ($puce->resolution != null){
			$query.= "resolution = :resolution , ";
		}
		if ($puce->taillemin != null){
			$query.= "taillemin = :taillemin , ";
		}
		if ($puce->nbmqdeviant != null){
			$query.= "nbmqdeviant = :nbmqdeviant , ";
		}
		if ($puce->qualite != null){
			$query.= "qualite = :qualite , ";
		}
		if ($puce->versionhg != null){
			$query.= "versionhg = :versionhg , ";
		}
		if ($puce->iddossier != null){
			$query.= "iddossier = :iddossier , ";
		}
		$query = substr($query, 0,-2);
		$query = $query." WHERE idpuce= :idpuce";
		$requette_preparee = $this->connexion->prepare($query);
		if ($puce->idtypepuce != null){
		$requette_preparee->bindValue(":idtypepuce" , $puce->idtypepuce, PDO::PARAM_INT);
		}
		if ($puce->idtypeprelevement != null){
			$requette_preparee->bindValue(":idtypeprelevement" , $puce->idtypeprelevement, PDO::PARAM_INT);
		}
		if ($puce->formulechromosomique != null){
		$requette_preparee->bindValue(":formulechromosomique" , $puce->formulechromosomique, PDO::PARAM_STR);
		}
		if ($puce->analyse != null){
		$requette_preparee->bindValue(":analyse" , $puce->analyse, PDO::PARAM_STR);
		}
		if ($puce->resolution != null){
			$requette_preparee->bindValue(":resolution" , $puce->resolution, PDO::PARAM_STR);
		}
		if ($puce->taillemin != null){
			$requette_preparee->bindValue(":taillemin" , $puce->taillemin, PDO::PARAM_STR);
		};
		if ($puce->nbmqdeviant != null){
			$requette_preparee->bindValue(":nbmqdeviant" , $puce->nbmqdeviant, PDO::PARAM_STR);
		};
		if ($puce->qualite != null){
			$requette_preparee->bindValue(":qualite" , $puce->qualite, PDO::PARAM_STR);
		}
			
		if ($puce->versionhg != null){
			$requette_preparee->bindValue(":versionhg" , $puce->versionhg, PDO::PARAM_STR);
		}
		if ($puce->iddossier != null){
			$requette_preparee->bindValue(":iddossier" , $puce->iddossier, PDO::PARAM_INT);
		}
		$requette_preparee->bindValue(":idpuce" , $puce->idpuce, PDO::PARAM_INT);
		$t = $requette_preparee->execute();
	
		if(!$t)
		{
			
			$taberr = $requette_preparee->errorInfo();
			$message = "Erreur numéro :".$taberr[0]."(".$taberr[1].")".$query;
	
		}else {
			$message = $puce->idpuce;
		}
		return $message;
	}
	

} // PuceDAO



?>
